package com.example.jqying.androiddemo.webview;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.example.jqying.androiddemo.R;

public class WebVIewActivity extends AppCompatActivity {

    private WebView mWvTest;
    private int startX;
    private int scrollSize = 700;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        mWvTest = (WebView) findViewById(R.id.wv_test);
//        mWvTest.loadUrl("file:///android_asset/test.html");
        mWvTest.getSettings().setJavaScriptEnabled(true);
        mWvTest.setWebViewClient(new MyWebViewClient());        //处理网页加载前后等事件
        mWvTest.setWebChromeClient(new WebChromeClient());      //辅助处理对话框等事件
        mWvTest.loadUrl("https://m.baidu.com");

        /*
        *   配置滑动前进后退事件
         */
        mWvTest.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        startX = (int) event.getX();
                        break;
                    case MotionEvent.ACTION_UP:
                        int endX = (int) event.getX();
                        if(endX>startX && mWvTest.canGoBack() && endX-startX>scrollSize){
                            mWvTest.goBack();
                        }else if(endX<startX &&mWvTest.canGoForward() && startX-endX>scrollSize){
                            mWvTest.goForward();
                        }
                        break;
                    default:
                        break;
                }
                return false;
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mWvTest.clearHistory();
        mWvTest.clearCache(true);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK && mWvTest.canGoBack()){
            mWvTest.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);

    }

    class MyWebViewClient extends WebViewClient{

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return super.shouldOverrideUrlLoading(view, request);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            //Toast.makeText(WebVIewActivity.this,"start",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            //Toast.makeText(WebVIewActivity.this,"Ok",Toast.LENGTH_SHORT).show();

        }

    }

    class MyWebChromeClient extends WebViewClient{

    }
}
